package simpSync.model;

import java.util.List;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.TextInputControl;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.web.WebView;
import javafx.util.Duration;

/**
 * Classe que representa um conteúdo multimidia
 */
public class Media {

    private String id;
    private String src;
    private String type;
    private String duration;
    private String region;
    private int startTime;
    private int endTime;

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @return the src
     */
    public String getSrc() {
        return src;
    }

    /**
     * @param src the src to set
     */
    public void setSrc(String src) {
        this.src = src;
    }

    /**
     * @return the type
     */
    public String getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(String type) {
        this.type = type;
    }

    /**
     * @return the duration
     */
    public String getDuration() {
        return duration;
    }

    /**
     * @param duration the duration to set
     */
    public void setDuration(String duration) {
        this.duration = duration;
    }

    /**
     * @return the region
     */
    public String getRegion() {
        return region;
    }

    /**
     * @param region the region to set
     */
    public void setRegion(String region) {
        this.region = region;
    }

    public void create(Pane pane, SimpleSync ss) {

        Node media = null;

        // Defino o media de acordo com o tipo
        // Poderia ser um factory (abstract factory pattern), mas nao quis fazer assim
        if (getType().equals("video")) {
            media = new MediaView(new MediaPlayer(new javafx.scene.media.Media(getSrc())));
            

            if (getStartTime() > 0) {
                ((MediaView) media).getMediaPlayer().setStartTime(new Duration(getStartTime()*1000)); // * 1000 para segundos
                ((MediaView) media).getMediaPlayer().play();
            } else {
                ((MediaView) media).getMediaPlayer().setAutoPlay(true);
            }
            
            if (getEndTime()>0)
            {
              ((MediaView) media).getMediaPlayer().setStopTime(new Duration(getEndTime()*1000));
            }


        } else if (getType().equals("img")) {
            media = new ImageView();
            ((ImageView) media).setImage(new Image(getSrc()));
            System.out.println(getSrc());
        } else if (getType().equals("html")) {
            media = new WebView();
            ((WebView) media).getEngine().load(getSrc());
        }


        

        if (media != null) {
                media.setId(this.getId());
            //Adicioando  o media no Pane correto (region)
            for (Region r : ss.getRegions()) {
                if (r.getId().equals(getRegion())) {
                    //registro o node no Painel
                    r.getPaneContent().getChildren().add(media);
                    //registro o node na lista de nodes
                    ss.getNodes().add(media);
                    break;
                }
            }
        }


    }

    public int getStartTime() {
        return startTime;
    }

    public void setStartTime(int startTime) {
        this.startTime = startTime;
    }

    public int getEndTime() {
        return endTime;
    }

    public void setEndTime(int endTime) {
        this.endTime = endTime;
    }
}
